﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SSN.BusinessFramework.Calendar;
using SSN.Utilities.Security;
using SSN.WebFramework;
namespace SSN.WebFrameworkTests
{
    [TestClass]
    public class CalendarTests
    {
        [TestMethod]
        public void CalendarCrud()
        {
            CalendarEvent ce = new CalendarEvent();
            TestSecurityContext ctx = new TestSecurityContext("thouk", new List<string>(){ "SSN IT"});
            ce.New(ctx);
            ce.Start = new DateTime(2012, 4, 1);
            ce.End = new DateTime(2012, 4, 1);
            ce.CategoryID = CalendarManager.Categories[0].ID;
            ce.Description = "";
            ce.Link = "http://www.test.com";
            ce.Priority = CalendarPriority.Medium;
            ce.Title = "Memorial Day";
            ce.Visibility = EventVisibility.Reps | EventVisibility.Employees;

            Assert.IsTrue(ce.Save());

            CalendarManager cm = new CalendarManager(ctx);
            cm.PurgeCache();
            Assert.IsTrue(cm.GetEventsForMonthYear(4, 2012, EventVisibility.Employees | EventVisibility.Reps).Count > 0);
            CalendarEvent ev2;
            ev2 = cm.GetEvent(ce.ID);
            Assert.IsTrue(ev2 != null);
            ev2.Delete();
            cm.PurgeCache();
            Assert.IsTrue(cm.GetEvent(ev2.ID) == null);
        }

        [TestMethod]
        public void CalendarManagement()
        {
            TestSecurityContext ctx = new TestSecurityContext("thouk", new List<string>() { "SSN IT" });
            
            DateTime now = DateTime.Now;
            List<CalendarEvent> ceList = new List<CalendarEvent>();
            for (int i = 0; i < 10; i++)
            {
                CalendarEvent ce = new CalendarEvent();
                ceList.Add(ce);
                ce.New(ctx);
                now = now.AddDays(4);
                ce.Start = new DateTime(now.Year, now.Month, now.Day);
                ce.End = new DateTime(now.Year, now.Month, now.Day);
                ce.CategoryID = CalendarManager.Categories[0].ID;
                ce.Description = "";
                ce.Link = "http://www.test.com";
                ce.Priority = CalendarPriority.Medium;
                ce.Title = "Holiday Day";
                ce.Visibility = EventVisibility.Reps | EventVisibility.Employees;
                ce.Save();
            }

            CalendarManager cmgr = new CalendarManager(ctx);
            ///Assuming that the upcoming events gets events for 3 weeks in advance
            Assert.IsTrue(cmgr.GetUpcomingEvents().Count == 5);
            foreach (CalendarEvent ce in ceList)
            {
                ce.Delete();
            }
        }
    }
}
